package com.c.ctgproject.ui.collect

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.ImageCommonRvV2Adapter
import com.c.ctgproject.adapter.guang.FirstCheckCountAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityCarFirshCheckBinding
import com.c.ctgproject.entity.guang.CarInfoEntityV2
import com.c.ctgproject.event.PositionEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import org.greenrobot.eventbus.EventBus

/**
 *作者:LiBW
 *创建日期: 2021/9/7
 *描述:车辆初检 页面
 */
class CarFirstCheckActivity : BaseActivityV2() {
    private val mBinding by lazy { ActivityCarFirshCheckBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root

    private val mCarId by lazy { intent.getIntExtra(MyConstants.CAR_ID, -1) }
    private val mListPosition by lazy { intent.getIntExtra(MyConstants.POSITION,-1) }

    override fun event() {
        ApiMethods.apiSubscribe(ApiMethods.retrofit.carDetailHasImage(mCarId), MyObserverHandlerLoading(this, loading) {
            MyLog.d("车辆详情回调:$it")
            val entity = getGson().fromJson(it, CarInfoEntityV2::class.java).data

            entity?.let { data ->
                mBinding.carSourceET.text = data.carSource.noDataHandler()
                mBinding.carVinET.text = data.carVin.noDataHandler()

                when (data.carStatus) {
                    1 -> mBinding.mCarStatueTV.text = "整车"
                    2 -> mBinding.mCarStatueTV.text = "车架"
                    else -> mBinding.mCarStatueTV.text = "暂无数据"
                }
                mBinding.carPlateET.text = data.carPlate.noDataHandler()
                mBinding.carModelET.text = data.carModel.noDataHandler()
                when (data.energy) {
                    1 -> mBinding.mBatteryModelTV.text = "燃油车"
                    2 -> mBinding.mBatteryModelTV.text = "电动车"
                    3 -> mBinding.mBatteryModelTV.text = "油电混合"
                    else -> mBinding.mBatteryModelTV.text = "暂无数据"
                }
                mBinding.mPreRemarkTV.text = data.preRemark.noDataHandler()

                when (data.hasBattery) {
                    1 -> mBinding.mYDC.performClick()
                    0 -> mBinding.mWDC.performClick()
                }
                val carType = when (data.carType) {
                    1 -> "社会车"
                    2 -> "试验车"
                    3 -> "外包车"
                    4 -> "五类车"
                    5 -> "农机具"
                    else -> "暂无数据"
                }
                mBinding.carType.text = carType

                //五类车不展示底盘数量等
                if (data.carType != 4) {
                    data.carPartsList?.let {
                        mBinding.mCountRV.adapter=FirstCheckCountAdapter(this,it)
                        mBinding.mCountRV.layoutManager=LinearLayoutManager(this)
                    }
                }
                if(data.carCheck==1){
                    mBinding.mConfirmBTN.visibility= View.GONE
                    mBinding.mCancelBTN.visibility=View.VISIBLE
                }else{
                    mBinding.mConfirmBTN.visibility= View.VISIBLE
                    mBinding.mCancelBTN.visibility=View.VISIBLE
                }
//                    mBinding.mPartLL.visibility = View.GONE
//                } else {
//
//
//
//                    mBinding.mPartLL.addView(CarInfoLayout(this, "发动机", data.engine.noDataHandler()))
//                    mBinding.mPartLL.addView(CarInfoLayout(this, "变速箱", data.transmissionCase.noDataHandler()))
//                    mBinding.mPartLL.addView(CarInfoLayout(this, "三元催化", data.ternaryCatalysis.noDataHandler()))
//                    mBinding.mPartLL.addView(CarInfoLayout(this, "蓄电池", data.battery.noDataHandler()))
//                    mBinding.mPartLL.addView(CarInfoLayout(this, "动力电池", data.powerBattery.noDataHandler()))
//                    mBinding.mPartLL.addView(CarInfoLayout(this, "耦合电机", data.couplingMotor.noDataHandler()))


//                    mBinding.ternaryCatalysisET.text = data.ternaryCatalysis.noDataHandler()
//                    mBinding.batteryET.text = data.battery.noDataHandler()
//                    mBinding.powerBatteryET.text = data.powerBattery.noDataHandler()
//                    mBinding.starterET.text = data.starter.noDataHandler()
//                    mBinding.alternatorET.text = data.alternator.noDataHandler()
//                    mBinding.waterTankET.text = data.waterTank.noDataHandler()
//                    mBinding.airConditionerET.text = data.airConditioner.noDataHandler()
//                    mBinding.couplingMotorET.text = data.couplingMotor.noDataHandler()
//                    mBinding.chassisET.text = data.chassis.noDataHandler()
//                    mBinding.engineET.text = data.engine.noDataHandler()
//                    mBinding.transmissionCaseET.text = data.transmissionCase.noDataHandler()
//                    mBinding.hubET.text = data.hub.noDataHandler()
//                    mBinding.tyreET.text = data.tyre.noDataHandler()
//                }

                //登记入库图片
                data.entrancePictures?.let { list ->
                    mBinding.mPhotoList.adapter = ImageCommonRvV2Adapter(this, list)
                    mBinding.mPhotoList.layoutManager = GridLayoutManager(this, 3, LinearLayoutManager.VERTICAL, false)
                }
            }
        })
    }

    override fun click() {
        mBinding.mConfirmBTN.setOnClickListener {
            submit(true)
        }
        mBinding.mCancelBTN.setOnClickListener {
            submit(false)
        }
    }

    private fun submit(success: Boolean) {
        val map = HashMap<String, Any>()
        map["carId"] = mCarId
        map["result"] = if (success) 1 else 2
        val remark = mBinding.mRemarkET.text.handlerNull()
        if (remark.isNotEmpty()) map["remark"] = remark
        ApiMethods.apiSubscribe(ApiMethods.retrofit.firstCheck(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
            MyLog.d("初检回调:$it")
            showToast("提交成功")
            EventBus.getDefault().post(PositionEvent(mListPosition))
            finish()
        })
    }
}